﻿using Stimulsoft.Report.Export;
using System.Collections.Generic;
using System.IO;
using System.Threading.Tasks;

namespace Sidvall.Reporting
{
    public class StimulsoftReport : Sidvall.Reporting.IReport
    {
        #region Public Members

        public async Task<Stream> ExportAsync(Stream reportStream, RenderingFormat format, IEnumerable<DataSource> dataSources, IEnumerable<Parameter> parameters)
        {
            var stream = new MemoryStream();
            var report = new Stimulsoft.Report.StiReport();
            report.Load(reportStream);
            if (dataSources != null)
            {
                foreach (var dataSource in dataSources)
                    report.RegBusinessObject(dataSource.Name, dataSource.Value);
            }
            if (parameters != null)
            {
                foreach (var parameter in parameters)
                {
                    if (report.Variables.ContainsKey(parameter.Name))
                        report.Variables[parameter.Name] = parameter.Value;
                }
            }
            report.Render(false);
            switch (format)
            {
                case Sidvall.Reporting.RenderingFormat.Pdf:
                    var pdfSettings = new StiPdfExportSettings()
                    {
                    };
                    report.ExportDocument(Stimulsoft.Report.StiExportFormat.Pdf, stream, pdfSettings);
                    break;
                default:
                    break;
            }
            return await Task.FromResult(stream).ConfigureAwait(false);
        }

        #endregion
    }
}
